/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webResource;

import com.google.gson.Gson;
import dao.BookingDAO;
import dao.FlightDAO;
import dao.ReceiptDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXB;
import model.Booking;
import model.BookingAndReceipt;
import model.Flight;
import model.Receipt;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * REST Web Service
 *
 * @author Jorge
 */
@Path("database")
public class ServiceDatabase {

    /**
     * Creates a new instance of ServiceDatabase
     */
    public ServiceDatabase() {
    }

    /**
     * Retrieves representation of an instance of webResource.ServiceDatabase
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public List<Flight> searchFlights(@QueryParam("from")String from, @QueryParam("to") String to,
    @QueryParam("departureDate") String departureDate, @QueryParam("arrivalDate") String arrivalDate) {
        // Creates FlightDAO instance
		FlightDAO flightDAO = new FlightDAO();
		// List of available departure flights
                
                
		List<Flight> departureFlights = flightDAO.getAvailableFlights(from, to, departureDate);
		
		// List of available return flights
		List<Flight> returnFlights = flightDAO.getAvailableFlights(to, from, arrivalDate);
		
		if (departureFlights.size() == 0 || returnFlights.size() == 0)
			return null;
		else
		{
			// Flights to be returned
			List<Flight> dump = new ArrayList<Flight>(departureFlights.size() + returnFlights.size());
			dump.addAll(departureFlights);
			dump.addAll(returnFlights);
			
                        return  dump;
		}
    }

    /**
     * PUT method for updating or creating an instance of ServiceDatabase
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String updateInformation(BookingAndReceipt bookingAndReceipt) throws JSONException 
	{
//            
//            BookingAndReceipt bAndr  = new Gson().fromJson(bookingAndReceipt, BookingAndReceipt.class);
            //JSONObject JsonO = new JSONObject(bookingAndReceipt);
        
            //BookingAndReceipt bookingAndReceipt = JAXB.unmarshal(bookingReceipt, BookingAndReceipt.class);
            Booking b  = bookingAndReceipt.getBooking();
            Receipt r = bookingAndReceipt.getReceipt();
            
		BookingDAO bookingDAO = new BookingDAO();		// BookingDAO instance
		bookingDAO.insert(b);						// Inserts the generated booking in the database
		
		ReceiptDAO receiptDAO = new ReceiptDAO();		// ReceiptDAO instance
		receiptDAO.insert(r);						// Inserts the generated receipt in the database
		
		// FIXME How this service would be implemented?
		return "true";
	}
}
